Skip to main content

Basic functions

Let's start with a simple function,

function sum(a, b) {
console.log(a + b);
}

sum(2, 3);

The above piece of code declares a function sum. By invoking this function sum(2, 3) which prints 5.

You can try with different values and it will print following results.

Return the result

In javascript every function returns a value, in defalult it is undefined. But user can override this condition.

The return keyword will quit/exit the function.

function sum(a, b) {
return a + b;
}

console.log(sum(2, 3));